<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>Mathematics Template</title>
<link href="../docs-assets/Breadcrumbs.css" rel="stylesheet" rev="stylesheet" type="text/css">
		<meta name="viewport" content="width=device-width initial-scale=1">
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<meta http-equiv="Content-Language" content="en-gb">

<link href="../docs-assets/Contents.css" rel="stylesheet" rev="stylesheet" type="text/css">
<link href="../docs-assets/Progress.css" rel="stylesheet" rev="stylesheet" type="text/css">
<link href="../docs-assets/Navigation.css" rel="stylesheet" rev="stylesheet" type="text/css">
<link href="../docs-assets/Fonts.css" rel="stylesheet" rev="stylesheet" type="text/css">
<link href="../docs-assets/Base.css" rel="stylesheet" rev="stylesheet" type="text/css">
<script>
MathJax = {
	tex: {
		inlineMath: '$', '$'], ['\\(', '\\)'
	},
	svg: {
		fontCache: 'global'
	}
};
</script>
<script type="text/javascript" id="MathJax-script" async
	src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-svg.js">
</script>

<link href="../docs-assets/Colours.css" rel="stylesheet" rev="stylesheet" type="text/css">
		
	</head>
	<body class="commentary-font">
		<nav role="navigation">
		<h1><a href="../index.html"><img src="../docs-assets/Inform.png" height=72> </a></h1>
<ul><li><a href="../index.html">home</a></li>
</ul><h2>Compiler</h2><ul>
<li><a href="../structure.html">structure</a></li>
<li><a href="../inbuildn.html">inbuild</a></li>
<li><a href="../inform7n.html">inform7</a></li>
<li><a href="../intern.html">inter</a></li>
<li><a href="../services.html">services</a></li>
<li><a href="../secrets.html">secrets</a></li>
</ul><h2>Other Tools</h2><ul>
<li><a href="../inblorbn.html">inblorb</a></li>
<li><a href="../inform6.html">inform6</a></li>
<li><a href="../inpolicyn.html">inpolicy</a></li>
</ul><h2>Resources</h2><ul>
<li><a href="../extensions.html">extensions</a></li>
<li><a href="../kits.html">kits</a></li>
</ul><h2>Repository</h2><ul>
<li><a href="https://github.com/ganelson/inform"><img src="../docs-assets/github.png" height=0> github</a></li>
</ul><h2>Related Projects</h2><ul>
<li><a href="https://github.com/ganelson/inweb"><img src="../docs-assets/github.png" height=0> inweb</a></li>
<li><a href="https://github.com/ganelson/intest"><img src="../docs-assets/github.png" height=0> intest</a></li>
</ul>
		</nav>
		<main role="main">
		<!-- Weave of 'Mathematics Template' generated by inweb -->
<div class="breadcrumbs">
    <ul class="crumbs"><li><a href="../index.html">Home</a></li><li><a href="../extensions.html">Kits</a></li><li><a href="index.html">Architecture32Kit</a></li><li><b>Mathematics Template</b></li></ul></div>
<p class="purpose">Mathematical functions, especially for real numbers.</p>

<ul class="toc"><li><a href="mttm.html#SP1">&#167;1. Unsigned integer comparison</a></li><li><a href="mttm.html#SP2">&#167;2. Fully random word</a></li><li><a href="mttm.html#SP3">&#167;3. Integer square root</a></li><li><a href="mttm.html#SP4">&#167;4. Integer cube root</a></li><li><a href="mttm.html#SP5">&#167;5. Printing reals</a></li></ul><hr class="tocbar">

<p class="commentary firstcommentary"><a id="SP1" class="paragraph-anchor"></a><b>&#167;1. Unsigned integer comparison.</b>Comparison of I6 integers is normally signed, that is, treating the word as
a twos-complement signed number, so that <span class="extract"><span class="extract-syntax">$FFFF</span></span> is less than <span class="extract"><span class="extract-syntax">0</span></span>, for
instance. If we want to construe words as being unsigned integers, or as
addresses, we need to compare them with the following routine, which returns
1 if \(x&gt;y\), 0 if \(x=y\) and \(-1\) if \(x&lt;y\).
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">[ </span><span class="identifier-syntax">UnsignedCompare</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">jleu</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">lesseq</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="constant-syntax">1</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    .</span><span class="identifier-syntax">lesseq</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">jeq</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">equal</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> -1;</span>
<span class="plain-syntax">    .</span><span class="identifier-syntax">equal</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>
</pre>
<p class="commentary firstcommentary"><a id="SP2" class="paragraph-anchor"></a><b>&#167;2. Fully random word.</b>This should be our best try at a single word consisting of 32 uniformly random bits.
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">[ </span><span class="identifier-syntax">FullyRandomWord</span><span class="plain-syntax"> </span><span class="identifier-syntax">w</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="reserved-syntax">random</span><span class="plain-syntax"> </span><span class="constant-syntax">0</span><span class="plain-syntax"> </span><span class="identifier-syntax">w</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">w</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>
</pre>
<p class="commentary firstcommentary"><a id="SP3" class="paragraph-anchor"></a><b>&#167;3. Integer square root.</b>Although this routine performs integer square root, it does so using Glulx's
floating-point operations if available (with code contributed by Andrew
Plotkin): this is fast and remains accurate up to about 16 million.
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">[ </span><span class="identifier-syntax">VM_SquareRoot</span><span class="plain-syntax"> </span><span class="identifier-syntax">num</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">numtof</span><span class="plain-syntax"> </span><span class="identifier-syntax">num</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">sqrt</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">ftonumz</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">num</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">num</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>
</pre>
<p class="commentary firstcommentary"><a id="SP4" class="paragraph-anchor"></a><b>&#167;4. Integer cube root.</b>The following, again, uses floating-point arithmetic if it's available:
this is fast and gives good accuracy for smallish numbers, but limited
precision begins to tell at around 2000000.
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">[ </span><span class="identifier-syntax">VM_CubeRoot</span><span class="plain-syntax"> </span><span class="identifier-syntax">num</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">neg</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">num</span><span class="plain-syntax"> &lt; </span><span class="constant-syntax">0</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">neg</span><span class="plain-syntax"> = </span><span class="reserved-syntax">true</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">num</span><span class="plain-syntax"> = -</span><span class="identifier-syntax">num</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">numtof</span><span class="plain-syntax"> </span><span class="identifier-syntax">num</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">pow</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="constant-syntax">1051372203</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax">; </span><span class="comment-syntax">pow(x, 0.3333)</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">ftonumz</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">num</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">neg</span><span class="plain-syntax">)</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">return</span><span class="plain-syntax"> -</span><span class="identifier-syntax">num</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">else</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">num</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>
</pre>
<p class="commentary firstcommentary"><a id="SP5" class="paragraph-anchor"></a><b>&#167;5. Printing reals.</b>Most of the code in this section is by Andrew Plotkin, and derives from test
cases used to check the floating-point extensions to Glulx.
</p>

<pre class="displayed-code all-displayed-code code-font">
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Say</span><span class="plain-syntax"> </span><span class="identifier-syntax">fp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">print</span><span class="plain-syntax"> (</span><span class="identifier-syntax">Float</span><span class="plain-syntax">) </span><span class="identifier-syntax">fp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>

<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Compare</span><span class="plain-syntax"> </span><span class="identifier-syntax">r1</span><span class="plain-syntax"> </span><span class="identifier-syntax">r2</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">jflt</span><span class="plain-syntax"> </span><span class="identifier-syntax">r1</span><span class="plain-syntax"> </span><span class="identifier-syntax">r2</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">less</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">jfeq</span><span class="plain-syntax"> </span><span class="identifier-syntax">r1</span><span class="plain-syntax"> </span><span class="identifier-syntax">r2</span><span class="plain-syntax"> </span><span class="constant-syntax">0</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">same</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="constant-syntax">1</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    .</span><span class="identifier-syntax">same</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    .</span><span class="identifier-syntax">less</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> -1;</span>
<span class="plain-syntax">];</span>

<span class="plain-syntax">[ </span><span class="identifier-syntax">NUMBER_TY_to_REAL_NUMBER_TY</span><span class="plain-syntax"> </span><span class="identifier-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">real</span><span class="plain-syntax">; @</span><span class="identifier-syntax">numtof</span><span class="plain-syntax"> </span><span class="identifier-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">real</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">real</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_to_NUMBER_TY</span><span class="plain-syntax"> </span><span class="identifier-syntax">real</span><span class="plain-syntax"> </span><span class="identifier-syntax">int</span><span class="plain-syntax">; @</span><span class="identifier-syntax">ftonumn</span><span class="plain-syntax"> </span><span class="identifier-syntax">real</span><span class="plain-syntax"> </span><span class="identifier-syntax">int</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">int</span><span class="plain-syntax">; ];</span>

<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Sin</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">sin</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Cos</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">cos</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Tan</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">tan</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Arcsin</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">asin</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Arccos</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">acos</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Arctan</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">atan</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>

<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Sinh</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">tmp</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">exp</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">tmp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fsub</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_0</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">exp</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fsub</span><span class="plain-syntax"> </span><span class="identifier-syntax">tmp</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fmul</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_HALF</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>

<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Cosh</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">tmp</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">exp</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">tmp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fsub</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_0</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">exp</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fadd</span><span class="plain-syntax"> </span><span class="identifier-syntax">tmp</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fmul</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_HALF</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>

<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Tanh</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">s</span><span class="plain-syntax"> </span><span class="identifier-syntax">c</span><span class="plain-syntax"> </span><span class="identifier-syntax">t</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">s</span><span class="plain-syntax"> = </span><span class="identifier-syntax">REAL_NUMBER_TY_Sinh</span><span class="plain-syntax">(</span><span class="reserved-syntax">in</span><span class="plain-syntax">);</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">c</span><span class="plain-syntax"> = </span><span class="identifier-syntax">REAL_NUMBER_TY_Cosh</span><span class="plain-syntax">(</span><span class="reserved-syntax">in</span><span class="plain-syntax">);</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fdiv</span><span class="plain-syntax"> </span><span class="identifier-syntax">s</span><span class="plain-syntax"> </span><span class="identifier-syntax">c</span><span class="plain-syntax"> </span><span class="identifier-syntax">t</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">t</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>

<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Reciprocal</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">fdiv</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_1</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Negate</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">fsub</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_0</span><span class="plain-syntax"> </span><span class="reserved-syntax">in</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Plus</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">fadd</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Minus</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">fsub</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Times</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">fmul</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Divide</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">fdiv</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Remainder</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">r</span><span class="plain-syntax"> </span><span class="identifier-syntax">q</span><span class="plain-syntax">; @</span><span class="identifier-syntax">fmod</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">r</span><span class="plain-syntax"> </span><span class="identifier-syntax">q</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">r</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Approximate</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">quotient</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fdiv</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">quotient</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fadd</span><span class="plain-syntax"> </span><span class="identifier-syntax">quotient</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_HALF</span><span class="plain-syntax"> </span><span class="identifier-syntax">quotient</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">floor</span><span class="plain-syntax"> </span><span class="identifier-syntax">quotient</span><span class="plain-syntax"> </span><span class="identifier-syntax">quotient</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fmul</span><span class="plain-syntax"> </span><span class="identifier-syntax">quotient</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Root</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">sqrt</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Cube_Root</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">pow</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_THIRD</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Pow</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">pow</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">y</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Exp</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">exp</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Log</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">log</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_BLog</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">n</span><span class="plain-syntax"> </span><span class="identifier-syntax">d</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">log</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">n</span><span class="plain-syntax"> == </span><span class="constant-syntax">10</span><span class="plain-syntax">) </span><span class="identifier-syntax">d</span><span class="plain-syntax"> = </span><span class="identifier-syntax">M_LOG10</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">else</span><span class="plain-syntax"> {</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">numtof</span><span class="plain-syntax"> </span><span class="identifier-syntax">n</span><span class="plain-syntax"> </span><span class="identifier-syntax">d</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">log</span><span class="plain-syntax"> </span><span class="identifier-syntax">d</span><span class="plain-syntax"> </span><span class="identifier-syntax">d</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fdiv</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax"> </span><span class="identifier-syntax">d</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Floor</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">floor</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Ceiling</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; @</span><span class="identifier-syntax">ceil</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">out</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Abs</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax">; </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> &amp; </span><span class="constant-syntax">$7fffffff</span><span class="plain-syntax">; ];</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">REAL_NUMBER_TY_Nan</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax">; @</span><span class="identifier-syntax">jisnan</span><span class="plain-syntax"> </span><span class="identifier-syntax">x</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">Nan</span><span class="plain-syntax">; </span><span class="reserved-syntax">rfalse</span><span class="plain-syntax">; .</span><span class="identifier-syntax">Nan</span><span class="plain-syntax">; </span><span class="reserved-syntax">rtrue</span><span class="plain-syntax">; ];</span>

<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_0</span><span class="plain-syntax">    = </span><span class="constant-syntax">$0</span><span class="plain-syntax">;</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_1</span><span class="plain-syntax">    = </span><span class="constant-syntax">$3F800000</span><span class="plain-syntax">;</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_HALF</span><span class="plain-syntax"> = </span><span class="constant-syntax">$3F000000</span><span class="plain-syntax">; </span><span class="comment-syntax">1/3</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_THIRD</span><span class="plain-syntax"> = </span><span class="constant-syntax">$3EAAAAAB</span><span class="plain-syntax">; </span><span class="comment-syntax">1/3</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_LOG10</span><span class="plain-syntax"> = </span><span class="constant-syntax">$40135D8E</span><span class="plain-syntax">; </span><span class="comment-syntax">log(10)</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_N1</span><span class="plain-syntax">   = </span><span class="constant-syntax">$BF800000</span><span class="plain-syntax">; </span><span class="comment-syntax">-1</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_PI</span><span class="plain-syntax">   = </span><span class="constant-syntax">$40490FDB</span><span class="plain-syntax">;</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_NPI</span><span class="plain-syntax">  = </span><span class="constant-syntax">$C0490FDB</span><span class="plain-syntax">;</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_2PI</span><span class="plain-syntax">  = </span><span class="constant-syntax">$40C90FDB</span><span class="plain-syntax">; </span><span class="comment-syntax">2*pi</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_PI2</span><span class="plain-syntax">  = </span><span class="constant-syntax">$3FC90FDB</span><span class="plain-syntax">; </span><span class="comment-syntax">pi/2</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_NPI2</span><span class="plain-syntax"> = </span><span class="constant-syntax">$BFC90FDB</span><span class="plain-syntax">;</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_E</span><span class="plain-syntax">    = </span><span class="constant-syntax">$402DF854</span><span class="plain-syntax">;</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_E2</span><span class="plain-syntax">   = </span><span class="constant-syntax">$40EC7326</span><span class="plain-syntax">; </span><span class="comment-syntax">e^2</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_N0</span><span class="plain-syntax">   = </span><span class="constant-syntax">$80000000</span><span class="plain-syntax">; </span><span class="comment-syntax">negative zero</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_INF</span><span class="plain-syntax">  = </span><span class="constant-syntax">$7F800000</span><span class="plain-syntax">; </span><span class="comment-syntax">infinity</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_NINF</span><span class="plain-syntax"> = </span><span class="constant-syntax">$FF800000</span><span class="plain-syntax">; </span><span class="comment-syntax">negative infinity</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_NAN</span><span class="plain-syntax">  = </span><span class="constant-syntax">$7F800001</span><span class="plain-syntax">; </span><span class="comment-syntax">one of many NaN values</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">M_NNAN</span><span class="plain-syntax"> = </span><span class="constant-syntax">$FF800001</span><span class="plain-syntax">; </span><span class="comment-syntax">another, with a sign bit</span>

<span class="comment-syntax">The Inform 6 compiler auto-defines these, but we're defining them here for</span>
<span class="comment-syntax">the benefit of other final compilation targets where that won't be the case.</span>

<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">FLOAT_INFINITY</span><span class="plain-syntax">  = </span><span class="constant-syntax">$7F800000</span><span class="plain-syntax">;</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">FLOAT_NINFINITY</span><span class="plain-syntax"> = </span><span class="constant-syntax">$FF800000</span><span class="plain-syntax">;</span>
<span class="reserved-syntax">Constant</span><span class="plain-syntax"> </span><span class="identifier-syntax">FLOAT_NAN</span><span class="plain-syntax">       = </span><span class="constant-syntax">$7FC00000</span><span class="plain-syntax">;</span>

<span class="comment-syntax">Floating-point parsing routines.</span>

<span class="comment-syntax">Parse a float from a text buffer. Returns a float value, or FLOAT_NAN if</span>
<span class="comment-syntax">no value was understood.</span>

<span class="comment-syntax">The recognized format, if you'll pardon a slightly bastardized regexp</span>
<span class="comment-syntax">syntax, is "S?D*(PD*)?(ES?D+)?" where S is a sign character "+" or "-",</span>
<span class="comment-syntax">D is a decimal digit "0" to "9", P is a decimal point ".",</span>
<span class="comment-syntax">and E is the exponential modifier "E" or "e".</span>

<span class="comment-syntax">For flexibility, the string "M10^" is also accepted for E, where M is</span>
<span class="comment-syntax">"X", "x", "*", or the multiplication sign @{D7}. Optional spaces are</span>
<span class="comment-syntax">allowed before and after the M sign. (But only for the "10^" form of</span>
<span class="comment-syntax">the exponent, not the "e" form.)</span>

<span class="comment-syntax">This routine does not try to recognize special names for infinity or NaN,</span>
<span class="comment-syntax">but it can return FLOAT_INFINITY or FLOAT_NINFINITY if the exponent is too</span>
<span class="comment-syntax">large.</span>

<span class="comment-syntax">This routine relies on floating-point math. Therefore, the same string</span>
<span class="comment-syntax">may parse to slightly different float values on different interpreters!</span>
<span class="comment-syntax">Be warned.</span>

<span class="comment-syntax">If useall is true, this insists on using all len characters from the buffer.</span>
<span class="comment-syntax">(It returns FLOAT_NAN if any unrecognized characters are left over.)</span>
<span class="comment-syntax">Contrariwise, if useall is false, unused characters at the end of the buffer</span>
<span class="comment-syntax">are fine. (But not at the beginning; the float must start at the beginning</span>
<span class="comment-syntax">of the buffer.)</span>

<span class="plain-syntax">[ </span><span class="identifier-syntax">FloatParse</span><span class="plain-syntax"> </span><span class="identifier-syntax">buf</span><span class="plain-syntax"> </span><span class="identifier-syntax">len</span><span class="plain-syntax"> </span><span class="identifier-syntax">useall</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">res</span><span class="plain-syntax"> </span><span class="identifier-syntax">ix</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> </span><span class="identifier-syntax">ch</span><span class="plain-syntax"> </span><span class="identifier-syntax">ten</span><span class="plain-syntax"> </span><span class="identifier-syntax">negative</span><span class="plain-syntax"> </span><span class="identifier-syntax">intpart</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracpart</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracdiv</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">expon</span><span class="plain-syntax"> </span><span class="identifier-syntax">expnegative</span><span class="plain-syntax"> </span><span class="identifier-syntax">count</span><span class="plain-syntax">;</span>

<span class="comment-syntax">print "FloatParse &lt;";</span>
<span class="comment-syntax">for (ix=0: ix&lt;len: ix++) print (char) buf--&gt;ix;</span>
<span class="comment-syntax">print "&gt;^";</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">len</span><span class="plain-syntax"> == </span><span class="constant-syntax">0</span><span class="plain-syntax">)</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FLOAT_NAN</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="identifier-syntax">ix</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">negative</span><span class="plain-syntax"> = </span><span class="reserved-syntax">false</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">intpart</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">fracpart</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">numtof</span><span class="plain-syntax"> </span><span class="constant-syntax">10</span><span class="plain-syntax"> </span><span class="identifier-syntax">ten</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="comment-syntax">Sign character (optional)</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">ch</span><span class="plain-syntax"> = </span><span class="identifier-syntax">buf</span><span class="plain-syntax">--&gt;</span><span class="identifier-syntax">ix</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ch</span><span class="plain-syntax"> == </span><span class="character-syntax">'-'</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">negative</span><span class="plain-syntax"> = </span><span class="reserved-syntax">true</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">    }</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">else</span><span class="plain-syntax"> </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ch</span><span class="plain-syntax"> == </span><span class="character-syntax">'+'</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">    }</span>

<span class="plain-syntax">    </span><span class="comment-syntax">Some digits (optional)</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">for</span><span class="plain-syntax"> (</span><span class="identifier-syntax">count</span><span class="plain-syntax">=0 : </span><span class="identifier-syntax">ix</span><span class="plain-syntax">&lt;</span><span class="identifier-syntax">len</span><span class="plain-syntax"> : </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++, </span><span class="identifier-syntax">count</span><span class="plain-syntax">++) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">ch</span><span class="plain-syntax"> = </span><span class="identifier-syntax">buf</span><span class="plain-syntax">--&gt;</span><span class="identifier-syntax">ix</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ch</span><span class="plain-syntax"> &lt; </span><span class="character-syntax">'0'</span><span class="plain-syntax"> || </span><span class="identifier-syntax">ch</span><span class="plain-syntax"> &gt; </span><span class="character-syntax">'9'</span><span class="plain-syntax">)</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">break</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">val</span><span class="plain-syntax"> = (</span><span class="identifier-syntax">ch</span><span class="plain-syntax"> - </span><span class="character-syntax">'0'</span><span class="plain-syntax">);</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">numtof</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">fmul</span><span class="plain-syntax"> </span><span class="identifier-syntax">intpart</span><span class="plain-syntax"> </span><span class="identifier-syntax">ten</span><span class="plain-syntax"> </span><span class="identifier-syntax">intpart</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">fadd</span><span class="plain-syntax"> </span><span class="identifier-syntax">intpart</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> </span><span class="identifier-syntax">intpart</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>

<span class="plain-syntax">    </span><span class="comment-syntax">Decimal point and more digits (optional)</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ix</span><span class="plain-syntax">&lt;</span><span class="identifier-syntax">len</span><span class="plain-syntax"> &amp;&amp; </span><span class="identifier-syntax">buf</span><span class="plain-syntax">--&gt;</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> == </span><span class="character-syntax">'.'</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">numtof</span><span class="plain-syntax"> </span><span class="constant-syntax">1</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracdiv</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">for</span><span class="plain-syntax"> ( : </span><span class="identifier-syntax">ix</span><span class="plain-syntax">&lt;</span><span class="identifier-syntax">len</span><span class="plain-syntax"> : </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++, </span><span class="identifier-syntax">count</span><span class="plain-syntax">++) {</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">ch</span><span class="plain-syntax"> = </span><span class="identifier-syntax">buf</span><span class="plain-syntax">--&gt;</span><span class="identifier-syntax">ix</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ch</span><span class="plain-syntax"> &lt; </span><span class="character-syntax">'0'</span><span class="plain-syntax"> || </span><span class="identifier-syntax">ch</span><span class="plain-syntax"> &gt; </span><span class="character-syntax">'9'</span><span class="plain-syntax">)</span>
<span class="plain-syntax">                </span><span class="reserved-syntax">break</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">val</span><span class="plain-syntax"> = (</span><span class="identifier-syntax">ch</span><span class="plain-syntax"> - </span><span class="character-syntax">'0'</span><span class="plain-syntax">);</span>
<span class="plain-syntax">            @</span><span class="identifier-syntax">numtof</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            @</span><span class="identifier-syntax">fmul</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracpart</span><span class="plain-syntax"> </span><span class="identifier-syntax">ten</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracpart</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            @</span><span class="identifier-syntax">fadd</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracpart</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracpart</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            @</span><span class="identifier-syntax">fmul</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracdiv</span><span class="plain-syntax"> </span><span class="identifier-syntax">ten</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracdiv</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        }</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">fdiv</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracpart</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracdiv</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracpart</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>

<span class="plain-syntax">    </span><span class="comment-syntax">If there are no digits before *or* after the decimal point, fail.</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">count</span><span class="plain-syntax"> == </span><span class="constant-syntax">0</span><span class="plain-syntax">)</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FLOAT_NAN</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="comment-syntax">Combine the integer and fractional parts.</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fadd</span><span class="plain-syntax"> </span><span class="identifier-syntax">intpart</span><span class="plain-syntax"> </span><span class="identifier-syntax">fracpart</span><span class="plain-syntax"> </span><span class="identifier-syntax">res</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="comment-syntax">Exponent (optional)</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ix</span><span class="plain-syntax">&lt;</span><span class="identifier-syntax">len</span><span class="plain-syntax"> &amp;&amp; </span><span class="identifier-syntax">buf</span><span class="plain-syntax">--&gt;</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> == </span><span class="character-syntax">'e'</span><span class="plain-syntax"> </span><span class="reserved-syntax">or</span><span class="plain-syntax"> </span><span class="character-syntax">'E'</span><span class="plain-syntax"> </span><span class="reserved-syntax">or</span><span class="plain-syntax"> </span><span class="character-syntax">' '</span><span class="plain-syntax"> </span><span class="reserved-syntax">or</span><span class="plain-syntax"> </span><span class="character-syntax">'*'</span><span class="plain-syntax"> </span><span class="reserved-syntax">or</span><span class="plain-syntax"> </span><span class="character-syntax">'x'</span><span class="plain-syntax"> </span><span class="reserved-syntax">or</span><span class="plain-syntax"> </span><span class="character-syntax">'X'</span><span class="plain-syntax"> </span><span class="reserved-syntax">or</span><span class="plain-syntax"> </span><span class="constant-syntax">$D7</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">buf</span><span class="plain-syntax">--&gt;</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> == </span><span class="character-syntax">'e'</span><span class="plain-syntax"> </span><span class="reserved-syntax">or</span><span class="plain-syntax"> </span><span class="character-syntax">'E'</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">            </span><span class="comment-syntax">no spaces, just the 'e'</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> == </span><span class="identifier-syntax">len</span><span class="plain-syntax">)</span>
<span class="plain-syntax">                </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FLOAT_NAN</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        }</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">else</span><span class="plain-syntax"> {</span>
<span class="plain-syntax">            </span><span class="comment-syntax">any number of spaces, "*", any number of spaces more, "10^"</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">while</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> &lt; </span><span class="identifier-syntax">len</span><span class="plain-syntax"> &amp;&amp; </span><span class="identifier-syntax">buf</span><span class="plain-syntax">--&gt;</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> == </span><span class="character-syntax">' '</span><span class="plain-syntax">)</span>
<span class="plain-syntax">                </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> == </span><span class="identifier-syntax">len</span><span class="plain-syntax">)</span>
<span class="plain-syntax">                </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FLOAT_NAN</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">buf</span><span class="plain-syntax">--&gt;</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> ~= </span><span class="character-syntax">'*'</span><span class="plain-syntax"> </span><span class="reserved-syntax">or</span><span class="plain-syntax"> </span><span class="character-syntax">'x'</span><span class="plain-syntax"> </span><span class="reserved-syntax">or</span><span class="plain-syntax"> </span><span class="character-syntax">'X'</span><span class="plain-syntax"> </span><span class="reserved-syntax">or</span><span class="plain-syntax"> </span><span class="constant-syntax">$D7</span><span class="plain-syntax">)</span>
<span class="plain-syntax">                </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FLOAT_NAN</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">while</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> &lt; </span><span class="identifier-syntax">len</span><span class="plain-syntax"> &amp;&amp; </span><span class="identifier-syntax">buf</span><span class="plain-syntax">--&gt;</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> == </span><span class="character-syntax">' '</span><span class="plain-syntax">)</span>
<span class="plain-syntax">                </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> == </span><span class="identifier-syntax">len</span><span class="plain-syntax">)</span>
<span class="plain-syntax">                </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FLOAT_NAN</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">buf</span><span class="plain-syntax">--&gt;</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> ~= </span><span class="character-syntax">'1'</span><span class="plain-syntax">)</span>
<span class="plain-syntax">                </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FLOAT_NAN</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">buf</span><span class="plain-syntax">--&gt;</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> ~= </span><span class="character-syntax">'0'</span><span class="plain-syntax">)</span>
<span class="plain-syntax">                </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FLOAT_NAN</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">buf</span><span class="plain-syntax">--&gt;</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> ~= </span><span class="constant-syntax">$5E</span><span class="plain-syntax">)</span>
<span class="plain-syntax">                </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FLOAT_NAN</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">        }</span>

<span class="plain-syntax">        </span><span class="comment-syntax">Sign character (optional)</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">expnegative</span><span class="plain-syntax"> = </span><span class="reserved-syntax">false</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">ch</span><span class="plain-syntax"> = </span><span class="identifier-syntax">buf</span><span class="plain-syntax">--&gt;</span><span class="identifier-syntax">ix</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ch</span><span class="plain-syntax"> == </span><span class="character-syntax">'-'</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">expnegative</span><span class="plain-syntax"> = </span><span class="reserved-syntax">true</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">        }</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">else</span><span class="plain-syntax"> </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ch</span><span class="plain-syntax"> == </span><span class="character-syntax">'+'</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">        }</span>

<span class="plain-syntax">        </span><span class="identifier-syntax">expon</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="comment-syntax">Some digits (mandatory)</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">for</span><span class="plain-syntax"> (</span><span class="identifier-syntax">count</span><span class="plain-syntax">=0 : </span><span class="identifier-syntax">ix</span><span class="plain-syntax">&lt;</span><span class="identifier-syntax">len</span><span class="plain-syntax"> : </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++, </span><span class="identifier-syntax">count</span><span class="plain-syntax">++) {</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">ch</span><span class="plain-syntax"> = </span><span class="identifier-syntax">buf</span><span class="plain-syntax">--&gt;</span><span class="identifier-syntax">ix</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ch</span><span class="plain-syntax"> &lt; </span><span class="character-syntax">'0'</span><span class="plain-syntax"> || </span><span class="identifier-syntax">ch</span><span class="plain-syntax"> &gt; </span><span class="character-syntax">'9'</span><span class="plain-syntax">)</span>
<span class="plain-syntax">                </span><span class="reserved-syntax">break</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">expon</span><span class="plain-syntax"> = </span><span class="constant-syntax">10</span><span class="plain-syntax">*</span><span class="identifier-syntax">expon</span><span class="plain-syntax"> + (</span><span class="identifier-syntax">ch</span><span class="plain-syntax"> - </span><span class="character-syntax">'0'</span><span class="plain-syntax">);</span>
<span class="plain-syntax">        }</span>

<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">count</span><span class="plain-syntax"> == </span><span class="constant-syntax">0</span><span class="plain-syntax">)</span>
<span class="plain-syntax">            </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FLOAT_NAN</span><span class="plain-syntax">;</span>

<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">expnegative</span><span class="plain-syntax">)</span>
<span class="plain-syntax">            </span><span class="identifier-syntax">expon</span><span class="plain-syntax"> = -</span><span class="identifier-syntax">expon</span><span class="plain-syntax">;</span>

<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">expon</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">            @</span><span class="identifier-syntax">numtof</span><span class="plain-syntax"> </span><span class="identifier-syntax">expon</span><span class="plain-syntax"> </span><span class="identifier-syntax">expon</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            @</span><span class="identifier-syntax">pow</span><span class="plain-syntax"> </span><span class="identifier-syntax">ten</span><span class="plain-syntax"> </span><span class="identifier-syntax">expon</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            @</span><span class="identifier-syntax">fmul</span><span class="plain-syntax"> </span><span class="identifier-syntax">res</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> </span><span class="identifier-syntax">res</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        }</span>
<span class="plain-syntax">    }</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">negative</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="comment-syntax">set the value's sign bit</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">res</span><span class="plain-syntax"> = </span><span class="constant-syntax">$80000000</span><span class="plain-syntax"> | </span><span class="identifier-syntax">res</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">useall</span><span class="plain-syntax"> &amp;&amp; </span><span class="identifier-syntax">ix</span><span class="plain-syntax"> ~= </span><span class="identifier-syntax">len</span><span class="plain-syntax">)</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FLOAT_NAN</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">res</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>

<span class="comment-syntax">Floating-point printing routines. (These are based on code in</span>
<span class="comment-syntax">Glulxercise.inf, but modified.)</span>

<span class="comment-syntax">Print a float. This uses exponential notation ("[-]N.NNNe[+-]NN") if</span>
<span class="comment-syntax">the exponent is not between 6 and -4. If it is (that is, if the</span>
<span class="comment-syntax">absolute value is near 1.0) then it uses decimal notation ("[-]NNN.NNNNN").</span>
<span class="comment-syntax">The precision is the number of digits after the decimal point</span>
<span class="comment-syntax">(at least one, no more than eight). The default is five, because</span>
<span class="comment-syntax">beyond that rounding errors creep in, and even exactly-represented</span>
<span class="comment-syntax">float values are printed with trailing fudgy digits.</span>
<span class="comment-syntax">Trailing zeroes are trimmed.</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">Float</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> </span><span class="identifier-syntax">prec</span><span class="plain-syntax">   </span><span class="identifier-syntax">pval</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">pval</span><span class="plain-syntax"> = </span><span class="identifier-syntax">val</span><span class="plain-syntax"> &amp; </span><span class="constant-syntax">$7FFFFFFF</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    @</span><span class="identifier-syntax">jz</span><span class="plain-syntax"> </span><span class="identifier-syntax">pval</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">UseFloatDec</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">jfge</span><span class="plain-syntax"> </span><span class="identifier-syntax">pval</span><span class="plain-syntax"> </span><span class="constant-syntax">$49742400</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">UseFloatExp</span><span class="plain-syntax">; </span><span class="comment-syntax">1000000.0</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">jflt</span><span class="plain-syntax"> </span><span class="identifier-syntax">pval</span><span class="plain-syntax"> </span><span class="constant-syntax">$38D1B717</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">UseFloatExp</span><span class="plain-syntax">; </span><span class="comment-syntax">0.0001</span>

<span class="plain-syntax">    .</span><span class="identifier-syntax">UseFloatDec</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FloatDec</span><span class="plain-syntax">(</span><span class="identifier-syntax">val</span><span class="plain-syntax">, </span><span class="identifier-syntax">prec</span><span class="plain-syntax">);</span>
<span class="plain-syntax">    .</span><span class="identifier-syntax">UseFloatExp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">return</span><span class="plain-syntax"> </span><span class="identifier-syntax">FloatExp</span><span class="plain-syntax">(</span><span class="identifier-syntax">val</span><span class="plain-syntax">, </span><span class="identifier-syntax">prec</span><span class="plain-syntax">);</span>
<span class="plain-syntax">];</span>

<span class="reserved-syntax">Array</span><span class="plain-syntax"> </span><span class="identifier-syntax">PowersOfTen</span><span class="plain-syntax"> --&gt; </span><span class="constant-syntax">1</span><span class="plain-syntax"> </span><span class="constant-syntax">10</span><span class="plain-syntax"> </span><span class="constant-syntax">100</span><span class="plain-syntax"> </span><span class="constant-syntax">1000</span><span class="plain-syntax"> </span><span class="constant-syntax">10000</span><span class="plain-syntax"> </span><span class="constant-syntax">100000</span><span class="plain-syntax"> </span><span class="constant-syntax">1000000</span><span class="plain-syntax"> </span><span class="constant-syntax">10000000</span><span class="plain-syntax"> </span><span class="constant-syntax">100000000</span><span class="plain-syntax"> </span><span class="constant-syntax">1000000000</span><span class="plain-syntax">;</span>

<span class="comment-syntax">Print a float in exponential notation: "[-]N.NNNe[+-]NN".</span>
<span class="comment-syntax">The precision is the number of digits after the decimal point</span>
<span class="comment-syntax">(at least one, no more than eight). The default is five, because</span>
<span class="comment-syntax">beyond that rounding errors creep in, and even exactly-represented</span>
<span class="comment-syntax">float values are printed with trailing fudgy digits.</span>
<span class="comment-syntax">Trailing zeroes are trimmed.</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">FloatExp</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> </span><span class="identifier-syntax">prec</span><span class="plain-syntax">   </span><span class="identifier-syntax">log10val</span><span class="plain-syntax"> </span><span class="identifier-syntax">expo</span><span class="plain-syntax"> </span><span class="identifier-syntax">fexpo</span><span class="plain-syntax"> </span><span class="identifier-syntax">idig</span><span class="plain-syntax"> </span><span class="identifier-syntax">ix</span><span class="plain-syntax"> </span><span class="identifier-syntax">pow10</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">prec</span><span class="plain-syntax"> == </span><span class="constant-syntax">0</span><span class="plain-syntax">)</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">prec</span><span class="plain-syntax"> = </span><span class="constant-syntax">5</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">prec</span><span class="plain-syntax"> &gt; </span><span class="constant-syntax">8</span><span class="plain-syntax">)</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">prec</span><span class="plain-syntax"> = </span><span class="constant-syntax">8</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">pow10</span><span class="plain-syntax"> = </span><span class="identifier-syntax">PowersOfTen</span><span class="plain-syntax"> --&gt; </span><span class="identifier-syntax">prec</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="comment-syntax">Knock off the sign bit first.</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">val</span><span class="plain-syntax"> &amp; </span><span class="constant-syntax">$80000000</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">streamchar</span><span class="plain-syntax"> </span><span class="character-syntax">'-'</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">val</span><span class="plain-syntax"> = </span><span class="identifier-syntax">val</span><span class="plain-syntax"> &amp; </span><span class="constant-syntax">$7FFFFFFF</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>

<span class="plain-syntax">    @</span><span class="identifier-syntax">jisnan</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">IsNan</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">jisinf</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">IsInf</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">val</span><span class="plain-syntax"> == </span><span class="constant-syntax">$0</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">expo</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">idig</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">jump</span><span class="plain-syntax"> </span><span class="identifier-syntax">DoPrint</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>

<span class="plain-syntax">    </span><span class="comment-syntax">Take as an example val=123.5, with precision=6. The desired</span>
<span class="plain-syntax">    </span><span class="comment-syntax">result is "1.23000e+02".</span>

<span class="plain-syntax">    @</span><span class="identifier-syntax">log</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fdiv</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="constant-syntax">$40135D8E</span><span class="plain-syntax"> </span><span class="identifier-syntax">log10val</span><span class="plain-syntax">; </span><span class="comment-syntax">$40135D8E is log(10)</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">floor</span><span class="plain-syntax"> </span><span class="identifier-syntax">log10val</span><span class="plain-syntax"> </span><span class="identifier-syntax">fexpo</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">ftonumn</span><span class="plain-syntax"> </span><span class="identifier-syntax">fexpo</span><span class="plain-syntax"> </span><span class="identifier-syntax">expo</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="comment-syntax">expo is now the exponent (as an integer). For our example, expo=2.</span>

<span class="plain-syntax">    @</span><span class="identifier-syntax">fsub</span><span class="plain-syntax"> </span><span class="identifier-syntax">log10val</span><span class="plain-syntax"> </span><span class="identifier-syntax">fexpo</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">numtof</span><span class="plain-syntax"> </span><span class="identifier-syntax">prec</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fadd</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fmul</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="constant-syntax">$40135D8E</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">exp</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="comment-syntax">The stack value is now exp((log10val - fexpo + prec) * log(10)).</span>
<span class="plain-syntax">    </span><span class="comment-syntax">We've shifted the decimal point left by expo digits (so that</span>
<span class="plain-syntax">    </span><span class="comment-syntax">it's after the first nonzero digit), and then right by prec</span>
<span class="plain-syntax">    </span><span class="comment-syntax">digits. In our example, that would be 1235000.0.</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">ftonumn</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="identifier-syntax">idig</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="comment-syntax">Round to an integer, and we have 1235000. Notice that this is</span>
<span class="plain-syntax">    </span><span class="comment-syntax">exactly the digits we want to print (if we stick a decimal point</span>
<span class="plain-syntax">    </span><span class="comment-syntax">after the first).</span>

<span class="plain-syntax">    .</span><span class="identifier-syntax">DoPrint</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">idig</span><span class="plain-syntax"> &gt;= </span><span class="constant-syntax">10</span><span class="plain-syntax">*</span><span class="identifier-syntax">pow10</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="comment-syntax">Rounding errors have left us outside the decimal range of</span>
<span class="plain-syntax">        </span><span class="comment-syntax">[1.0, 10.0) where we should be. Adjust to the next higher</span>
<span class="plain-syntax">        </span><span class="comment-syntax">exponent.</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">expo</span><span class="plain-syntax">++;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">div</span><span class="plain-syntax"> </span><span class="identifier-syntax">idig</span><span class="plain-syntax"> </span><span class="constant-syntax">10</span><span class="plain-syntax"> </span><span class="identifier-syntax">idig</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>

<span class="plain-syntax">    </span><span class="comment-syntax">Trim off trailing zeroes, as long as there's at least one digit</span>
<span class="plain-syntax">    </span><span class="comment-syntax">after the decimal point. (Delete this stanza if you want to</span>
<span class="plain-syntax">    </span><span class="comment-syntax">keep the trailing zeroes.)</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">while</span><span class="plain-syntax"> (</span><span class="identifier-syntax">prec</span><span class="plain-syntax"> &gt; </span><span class="constant-syntax">1</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">mod</span><span class="plain-syntax"> </span><span class="identifier-syntax">idig</span><span class="plain-syntax"> </span><span class="constant-syntax">10</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">jnz</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">DoneTrimming</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">div</span><span class="plain-syntax"> </span><span class="identifier-syntax">pow10</span><span class="plain-syntax"> </span><span class="constant-syntax">10</span><span class="plain-syntax"> </span><span class="identifier-syntax">pow10</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">div</span><span class="plain-syntax"> </span><span class="identifier-syntax">idig</span><span class="plain-syntax"> </span><span class="constant-syntax">10</span><span class="plain-syntax"> </span><span class="identifier-syntax">idig</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">prec</span><span class="plain-syntax">--;</span>
<span class="plain-syntax">    }</span>
<span class="plain-syntax">    .</span><span class="identifier-syntax">DoneTrimming</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">for</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ix</span><span class="plain-syntax">=0 : </span><span class="identifier-syntax">ix</span><span class="plain-syntax">&lt;=</span><span class="identifier-syntax">prec</span><span class="plain-syntax"> : </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++) {</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">div</span><span class="plain-syntax"> </span><span class="identifier-syntax">idig</span><span class="plain-syntax"> </span><span class="identifier-syntax">pow10</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">mod</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="constant-syntax">10</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">streamnum</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ix</span><span class="plain-syntax"> == </span><span class="constant-syntax">0</span><span class="plain-syntax">)</span>
<span class="plain-syntax">            @</span><span class="identifier-syntax">streamchar</span><span class="plain-syntax"> </span><span class="character-syntax">'.'</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">div</span><span class="plain-syntax"> </span><span class="identifier-syntax">pow10</span><span class="plain-syntax"> </span><span class="constant-syntax">10</span><span class="plain-syntax"> </span><span class="identifier-syntax">pow10</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>

<span class="plain-syntax">    </span><span class="comment-syntax">Print the exponent. There are two conventions coded here: the</span>
<span class="plain-syntax">    </span><span class="comment-syntax">engineering notation ("1.0e+00") and the mathematical ("1.0 x 10^0").</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">BasicInformKit</span><span class="plain-syntax">`</span><span class="identifier-syntax">PRINT_ENGINEER_EXPS_CFGF</span><span class="plain-syntax"> == </span><span class="constant-syntax">0</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">PrintMultiplicationSign</span><span class="plain-syntax">();</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">streamchar</span><span class="plain-syntax"> </span><span class="character-syntax">'1'</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">streamchar</span><span class="plain-syntax"> </span><span class="character-syntax">'0'</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">streamchar</span><span class="plain-syntax"> </span><span class="constant-syntax">$5E</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">streamnum</span><span class="plain-syntax"> </span><span class="identifier-syntax">expo</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    } </span><span class="reserved-syntax">else</span><span class="plain-syntax"> {</span>
<span class="plain-syntax">        </span><span class="comment-syntax">Convention is to use at least two digits.</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">streamchar</span><span class="plain-syntax"> </span><span class="character-syntax">'e'</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">expo</span><span class="plain-syntax"> &lt; </span><span class="constant-syntax">0</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">            @</span><span class="identifier-syntax">streamchar</span><span class="plain-syntax"> </span><span class="character-syntax">'-'</span><span class="plain-syntax">;</span>
<span class="plain-syntax">            @</span><span class="identifier-syntax">neg</span><span class="plain-syntax"> </span><span class="identifier-syntax">expo</span><span class="plain-syntax"> </span><span class="identifier-syntax">expo</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        }</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">else</span><span class="plain-syntax"> {</span>
<span class="plain-syntax">            @</span><span class="identifier-syntax">streamchar</span><span class="plain-syntax"> </span><span class="character-syntax">'+'</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        }</span>
<span class="plain-syntax">        </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">expo</span><span class="plain-syntax"> &lt; </span><span class="constant-syntax">10</span><span class="plain-syntax">)</span>
<span class="plain-syntax">            @</span><span class="identifier-syntax">streamchar</span><span class="plain-syntax"> </span><span class="character-syntax">'0'</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">streamnum</span><span class="plain-syntax"> </span><span class="identifier-syntax">expo</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">rtrue</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    .</span><span class="identifier-syntax">IsNan</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">PrintNan</span><span class="plain-syntax">();</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">rtrue</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    .</span><span class="identifier-syntax">IsInf</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">PrintInfinity</span><span class="plain-syntax">();</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">rtrue</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>

<span class="comment-syntax">Print a float in decimal notation: "[-]NNN.NNNNN".</span>
<span class="comment-syntax">The precision is the number of digits after the decimal point</span>
<span class="comment-syntax">(at least one, no more than eight). The default is five, because</span>
<span class="comment-syntax">beyond that rounding errors creep in, and even exactly-represented</span>
<span class="comment-syntax">float values are printed with trailing fudgy digits.</span>
<span class="comment-syntax">Trailing zeroes are trimmed.</span>
<span class="plain-syntax">[ </span><span class="identifier-syntax">FloatDec</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> </span><span class="identifier-syntax">prec</span><span class="plain-syntax">   </span><span class="identifier-syntax">log10val</span><span class="plain-syntax"> </span><span class="identifier-syntax">int</span><span class="plain-syntax"> </span><span class="identifier-syntax">fint</span><span class="plain-syntax"> </span><span class="identifier-syntax">extra0</span><span class="plain-syntax"> </span><span class="identifier-syntax">frac</span><span class="plain-syntax"> </span><span class="identifier-syntax">idig</span><span class="plain-syntax"> </span><span class="identifier-syntax">ix</span><span class="plain-syntax"> </span><span class="identifier-syntax">pow10</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">prec</span><span class="plain-syntax"> == </span><span class="constant-syntax">0</span><span class="plain-syntax">)</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">prec</span><span class="plain-syntax"> = </span><span class="constant-syntax">5</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">prec</span><span class="plain-syntax"> &gt; </span><span class="constant-syntax">8</span><span class="plain-syntax">)</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">prec</span><span class="plain-syntax"> = </span><span class="constant-syntax">8</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">pow10</span><span class="plain-syntax"> = </span><span class="identifier-syntax">PowersOfTen</span><span class="plain-syntax"> --&gt; </span><span class="identifier-syntax">prec</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="comment-syntax">Knock off the sign bit first.</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">val</span><span class="plain-syntax"> &amp; </span><span class="constant-syntax">$80000000</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">streamchar</span><span class="plain-syntax"> </span><span class="character-syntax">'-'</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">val</span><span class="plain-syntax"> = </span><span class="identifier-syntax">val</span><span class="plain-syntax"> &amp; </span><span class="constant-syntax">$7FFFFFFF</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>

<span class="plain-syntax">    @</span><span class="identifier-syntax">jisnan</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">IsNan</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">jisinf</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">IsInf</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="comment-syntax">Take as an example val=123.5, with precision=6. The desired result</span>
<span class="plain-syntax">    </span><span class="comment-syntax">is "123.50000".</span>

<span class="plain-syntax">    </span><span class="identifier-syntax">extra0</span><span class="plain-syntax"> = </span><span class="constant-syntax">0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fmod</span><span class="plain-syntax"> </span><span class="identifier-syntax">val</span><span class="plain-syntax"> </span><span class="constant-syntax">$3F800000</span><span class="plain-syntax"> </span><span class="identifier-syntax">frac</span><span class="plain-syntax"> </span><span class="identifier-syntax">fint</span><span class="plain-syntax">; </span><span class="comment-syntax">$3F800000 is 1.0.</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">ftonumz</span><span class="plain-syntax"> </span><span class="identifier-syntax">fint</span><span class="plain-syntax"> </span><span class="identifier-syntax">int</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="comment-syntax">This converts the integer part of the value to an integer value;</span>
<span class="plain-syntax">    </span><span class="comment-syntax">in our example, 123.</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">int</span><span class="plain-syntax"> == </span><span class="constant-syntax">$7FFFFFFF</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="comment-syntax">Looks like the integer part of the value is bigger than</span>
<span class="plain-syntax">        </span><span class="comment-syntax">we can store in an int variable. (It could be as large</span>
<span class="plain-syntax">        </span><span class="comment-syntax">as 3e+38.) We're going to have to use a log function to</span>
<span class="plain-syntax">        </span><span class="comment-syntax">reduce it by some number of factors of 10, and then pad</span>
<span class="plain-syntax">        </span><span class="comment-syntax">with zeroes.</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">log</span><span class="plain-syntax"> </span><span class="identifier-syntax">fint</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">fdiv</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="constant-syntax">$40135D8E</span><span class="plain-syntax"> </span><span class="identifier-syntax">log10val</span><span class="plain-syntax">; </span><span class="comment-syntax">$40135D8E is log(10)</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">ftonumz</span><span class="plain-syntax"> </span><span class="identifier-syntax">log10val</span><span class="plain-syntax"> </span><span class="identifier-syntax">extra0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">sub</span><span class="plain-syntax"> </span><span class="identifier-syntax">extra0</span><span class="plain-syntax"> </span><span class="constant-syntax">8</span><span class="plain-syntax"> </span><span class="identifier-syntax">extra0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="comment-syntax">extra0 is the number of zeroes we'll be padding with.</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">numtof</span><span class="plain-syntax"> </span><span class="identifier-syntax">extra0</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">fsub</span><span class="plain-syntax"> </span><span class="identifier-syntax">log10val</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">fmul</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="constant-syntax">$40135D8E</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">exp</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="comment-syntax">The stack value is now exp((log10val - extra0) * log(10)).</span>
<span class="plain-syntax">        </span><span class="comment-syntax">We've shifted the decimal point far enough left to leave</span>
<span class="plain-syntax">        </span><span class="comment-syntax">about eight digits, which is all we can print as an integer.</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">ftonumz</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="identifier-syntax">int</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>

<span class="plain-syntax">    </span><span class="comment-syntax">Print the integer part.</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">streamnum</span><span class="plain-syntax"> </span><span class="identifier-syntax">int</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">for</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ix</span><span class="plain-syntax">=0 : </span><span class="identifier-syntax">ix</span><span class="plain-syntax">&lt;</span><span class="identifier-syntax">extra0</span><span class="plain-syntax"> : </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++)</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">streamchar</span><span class="plain-syntax"> </span><span class="character-syntax">'0'</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    @</span><span class="identifier-syntax">streamchar</span><span class="plain-syntax"> </span><span class="character-syntax">'.'</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="comment-syntax">Now we need to print the frac part, which is .5.</span>

<span class="plain-syntax">    @</span><span class="identifier-syntax">log</span><span class="plain-syntax"> </span><span class="identifier-syntax">frac</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fdiv</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="constant-syntax">$40135D8E</span><span class="plain-syntax"> </span><span class="identifier-syntax">log10val</span><span class="plain-syntax">; </span><span class="comment-syntax">$40135D8E is log(10)</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">numtof</span><span class="plain-syntax"> </span><span class="identifier-syntax">prec</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fadd</span><span class="plain-syntax"> </span><span class="identifier-syntax">log10val</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">fmul</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="constant-syntax">$40135D8E</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">exp</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="comment-syntax">The stack value is now exp((frac + prec) * log(10)).</span>
<span class="plain-syntax">    </span><span class="comment-syntax">We've shifted the decimal point right by prec</span>
<span class="plain-syntax">    </span><span class="comment-syntax">digits. In our example, that would be 50000.0.</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">ftonumn</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="identifier-syntax">idig</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="comment-syntax">Round to an integer, and we have 50000. Notice that this is</span>
<span class="plain-syntax">    </span><span class="comment-syntax">exactly the (post-decimal-point) digits we want to print.</span>

<span class="plain-syntax">    .</span><span class="identifier-syntax">DoPrint</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    </span><span class="reserved-syntax">if</span><span class="plain-syntax"> (</span><span class="identifier-syntax">idig</span><span class="plain-syntax"> &gt;= </span><span class="identifier-syntax">pow10</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        </span><span class="comment-syntax">Rounding errors have left us outside the decimal range of</span>
<span class="plain-syntax">        </span><span class="comment-syntax">[0.0, 1.0) where we should be. I'm not sure this is possible,</span>
<span class="plain-syntax">        </span><span class="comment-syntax">actually, but we'll just adjust downward.</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">idig</span><span class="plain-syntax"> = </span><span class="identifier-syntax">pow10</span><span class="plain-syntax"> - </span><span class="constant-syntax">1</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>

<span class="plain-syntax">    </span><span class="comment-syntax">Trim off trailing zeroes, as long as there's at least one digit</span>
<span class="plain-syntax">    </span><span class="comment-syntax">after the decimal point. (Delete this stanza if you want to</span>
<span class="plain-syntax">    </span><span class="comment-syntax">keep the trailing zeroes.)</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">while</span><span class="plain-syntax"> (</span><span class="identifier-syntax">prec</span><span class="plain-syntax"> &gt; </span><span class="constant-syntax">1</span><span class="plain-syntax">) {</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">mod</span><span class="plain-syntax"> </span><span class="identifier-syntax">idig</span><span class="plain-syntax"> </span><span class="constant-syntax">10</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">jnz</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> ?</span><span class="identifier-syntax">DoneTrimming</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">div</span><span class="plain-syntax"> </span><span class="identifier-syntax">pow10</span><span class="plain-syntax"> </span><span class="constant-syntax">10</span><span class="plain-syntax"> </span><span class="identifier-syntax">pow10</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">div</span><span class="plain-syntax"> </span><span class="identifier-syntax">idig</span><span class="plain-syntax"> </span><span class="constant-syntax">10</span><span class="plain-syntax"> </span><span class="identifier-syntax">idig</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        </span><span class="identifier-syntax">prec</span><span class="plain-syntax">--;</span>
<span class="plain-syntax">    }</span>
<span class="plain-syntax">    .</span><span class="identifier-syntax">DoneTrimming</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    @</span><span class="identifier-syntax">div</span><span class="plain-syntax"> </span><span class="identifier-syntax">pow10</span><span class="plain-syntax"> </span><span class="constant-syntax">10</span><span class="plain-syntax"> </span><span class="identifier-syntax">pow10</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">for</span><span class="plain-syntax"> (</span><span class="identifier-syntax">ix</span><span class="plain-syntax">=0 : </span><span class="identifier-syntax">ix</span><span class="plain-syntax">&lt;</span><span class="identifier-syntax">prec</span><span class="plain-syntax"> : </span><span class="identifier-syntax">ix</span><span class="plain-syntax">++) {</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">div</span><span class="plain-syntax"> </span><span class="identifier-syntax">idig</span><span class="plain-syntax"> </span><span class="identifier-syntax">pow10</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">mod</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax"> </span><span class="constant-syntax">10</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">streamnum</span><span class="plain-syntax"> </span><span class="identifier-syntax">sp</span><span class="plain-syntax">;</span>
<span class="plain-syntax">        @</span><span class="identifier-syntax">div</span><span class="plain-syntax"> </span><span class="identifier-syntax">pow10</span><span class="plain-syntax"> </span><span class="constant-syntax">10</span><span class="plain-syntax"> </span><span class="identifier-syntax">pow10</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    }</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">rtrue</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    .</span><span class="identifier-syntax">IsNan</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">PrintNan</span><span class="plain-syntax">();</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">rtrue</span><span class="plain-syntax">;</span>

<span class="plain-syntax">    .</span><span class="identifier-syntax">IsInf</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="identifier-syntax">PrintInfinity</span><span class="plain-syntax">();</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">rtrue</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>

<span class="plain-syntax">[ </span><span class="identifier-syntax">PrintInfinity</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">streamunichar</span><span class="plain-syntax"> </span><span class="constant-syntax">$221E</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="comment-syntax">print "Inf";</span>
<span class="plain-syntax">];</span>

<span class="plain-syntax">[ </span><span class="identifier-syntax">PrintNan</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">streamunichar</span><span class="plain-syntax"> </span><span class="constant-syntax">$26a0</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="comment-syntax">print "NaN";</span>
<span class="plain-syntax">];</span>

<span class="plain-syntax">[ </span><span class="identifier-syntax">PrintMultiplicationSign</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">print</span><span class="plain-syntax"> </span><span class="string-syntax">" "</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    @</span><span class="identifier-syntax">streamunichar</span><span class="plain-syntax"> </span><span class="constant-syntax">$D7</span><span class="plain-syntax">;</span>
<span class="plain-syntax">    </span><span class="comment-syntax">print "x";</span>
<span class="plain-syntax">    </span><span class="reserved-syntax">print</span><span class="plain-syntax"> </span><span class="string-syntax">" "</span><span class="plain-syntax">;</span>
<span class="plain-syntax">];</span>
</pre>
<nav role="progress"><div class="progresscontainer">
    <ul class="progressbar"><li class="progressprev"><a href="sttm2.html">&#10094;</a></li><li class="progresssection"><a href="cptm.html">cptm</a></li><li class="progresssection"><a href="intm.html">intm</a></li><li class="progresssection"><a href="gltm.html">gltm</a></li><li class="progresssection"><a href="sttm.html">sttm</a></li><li class="progresssection"><a href="sttm2.html">sttm2</a></li><li class="progresscurrent">mttm</li><li class="progresssection"><a href="cst.html">cst</a></li><li class="progresssection"><a href="iot.html">iot</a></li><li class="progresssection"><a href="fiot.html">fiot</a></li><li class="progresssection"><a href="vntm.html">vntm</a></li><li class="progresssection"><a href="vnt.html">vnt</a></li><li class="progressnext"><a href="cst.html">&#10095;</a></li></ul></div>
</nav><!-- End of weave -->

		</main>
	</body>
</html>

